package com.util.change;

import org.springframework.util.StringUtils;

/**
 * 中英文变换
 *
 * @author zhu
 * @version 1.0
 * @date 2021/4/21 18:45
 */
public class ChineseAndEnglish {

    /**
     * 转全角的函数(SBC case)  <br/>
     * 全角空格为12288,半角空格为32，其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     * @param input  任意字符串
     * @return   全角字符串
     */
    public static String toSBC(String input) { // 半角转全角：
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 32) {
                c[i] = (char) 12288;
                continue;
            }
            if (c[i] < 127) {
                c[i] = (char) (c[i] + 65248);
            }
        }
        return new String(c);
    }

    /**
     * 转半角的函数(DBC case)<br/>
     * 全角空格为12288，半角空格为32
     * 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     * @param input 任意字符串
     * @return  半角字符串
     */
    public static String toDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375) {
                c[i] = (char) (c[i] - 65248);
            }
        }
        return new String(c);
    }

    //中文标点符号
    final static String[] chinesePunctuation = {"！", "，", "。", "；", "《", "》", "（", "）", "？",
            "｛", "｝", "“", "：", "【", "】", "”", "‘", "’", "、"};

    //英文标点符号
    final static String[] englishPunctuation = {"!", ",", ".", ";", "<", ">", "(", ")", "?",
            "{", "}", "\"", ":", "[", "]", "\"", "\'", "\'", "/"};

    /**
     * 中文标点符号转英文字标点符号
     *
     * @param str 原字符串
     * @return str 新字符串
     */
    public static final String chinesePunctuationToEnglish(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        for (int i = 0; i < chinesePunctuation.length; i++) {
            str = str.replaceAll(chinesePunctuation[i], englishPunctuation[i]);
        }
        return str;
    }

}
